home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9277 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  39 lines

  1. Path: ausnews.austin.ibm.com!usenet
  2. From: Leo Uzcategui <leou@austin.ibm.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: STL: for_each vs transform
  5. Date: Thu, 29 Feb 1996 12:53:37 -0600
  6. Organization: IBM Corp.
  7. Message-ID: <3135F631.41C6@austin.ibm.com>
  8. NNTP-Posting-Host: socks.austin.ibm.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (X11; I; AIX 1)
  13.  
  14. How can iterate thru a container, using for_each, and apply a function
  15. object which modifies each of the elements in the container?
  16. I tried passing the argument by name, but the template definition
  17. will not allow it.
  18.  
  19. Here's the example: Note addr and size values never change.
  20.  
  21. #include    <iostream.h>
  22. #include    <algo.h>
  23. #include    <vector.h>
  24.  
  25. struct S { vector<int> v; };
  26. struct g : public binary_function<S,int,int> {
  27.     int operator()(S s,int x) const
  28.     {s.v.push_back(x);
  29.     cout<<"x="<<x<<endl;
  30.     cout<<"g() addr of s:"<<(size_t)&s<<" size of v:"<<s.v.size()<<endl;
  31.     return 0;};
  32. };
  33.  
  34. void main() {
  35. vector<S> A(3); // A has 3 vector<int>'s
  36. for (int i=0; i<2; i++)
  37.     for_each( A.begin(),A.end(), bind2nd(g(),i) );
  38. }
  39.